package com.aube.model;

import com.huyn.baseframework.model.PicItem;
import com.huyn.baseframework.utils.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

/**
 * Created by huyaonan on 15/11/7.
 */
public class TimeLineItemDetail implements Serializable {
    /**
     * type = MULTICAMERA
     */
    public List<VideoItem> multiCinemaList;

    /**
     * add on Jan 11th 2017
     * 表示默认主镜头
     * 如果为空, 默认主镜头为当前
     * 如果不为空, 默认主镜头会切换到对应的镜头
     */
    public String focusId;

    public void initIndex() {
        if(multiCinemaList != null) {
            int index=0;
            for(VideoItem item : multiCinemaList) {
                item.index=index;
                index++;
            }
        }
    }

    public HashMap<String, String> getVideosByTime(long time) {
        if(multiCinemaList == null || multiCinemaList.size() == 0)
            return null;
        HashMap<String, String> videoIds = new HashMap<>();
        for(VideoItem item : multiCinemaList) {
            if(time >= item.delayTime && time <= item.delayTime+item.duration)
                videoIds.put(item.getVideoId(), item.getVideoId());
        }
        return videoIds;
    }

    public HashMap<String, VideoItem> getVideosMapByTime(long time) {
        if(multiCinemaList == null || multiCinemaList.size() == 0)
            return null;
        HashMap<String, VideoItem> videoIds = new HashMap<>();
        for(VideoItem item : multiCinemaList) {
            if(time >= item.delayTime && time <= item.delayTime+item.duration)
                videoIds.put(item.getVideoId(), item);
        }
        return videoIds;
    }

    public int getCommonDelay() {
        if(multiCinemaList == null || multiCinemaList.size() == 0)
            return 0;
        int delay = 10000;
        for(VideoItem item : multiCinemaList) {
            if(item.delayTime < delay)
                delay = item.delayTime;
        }

        for(VideoItem item : multiCinemaList) {
            item.updateDelay(delay);
        }
        return delay;
    }

    /**
     * type = QA,VS,VOTE
     */
    public String answer="";
    public int revealTheAnswer=0;
    public List<PicItem> picList;
    public List<OptionItem> optionList;

    public String getImgUrl() {
        return getImgUrl(PicItem.IMG_URL);
    }

    public String getImgTag() {
        return getImgUrl(PicItem.IMG_TAG);
    }

    private String getImgUrl(String type) {
        if(picList==null)
            return "";
        for(PicItem item : picList) {
            if(type.equalsIgnoreCase(item.type))
                return item.picurl;
        }
        return "";
    }

    /**
     * type = PIC
     */
    public PicItem pic;

    public void addToPicMap(HashMap<Integer, String> maps, int startTime) {
        if(pic != null) {
            //超过10s才提前，不然就算了
            if(startTime > 10) {
                startTime -= 10;
                maps.put(startTime, pic.picurl);
                startTime++;
            } else {
                return;
            }
        }

        if(picList==null)
            return;
        for(PicItem item : picList) {
            maps.put(startTime, item.picurl);
            startTime++;
        }
    }

    //新增卡片注释
    public String cardnote;

    //多镜头的倒计时时间
    public int timedown=0;

    public String getOptionId(int index) {
        if(optionList == null || optionList.size() <= index || index < 0)
            return null;
        return optionList.get(index).optionId;
    }

    public String getOptionValue(int index) {
        if(optionList == null || optionList.size() <= index)
            return "";
        return optionList.get(index).optionValue;
    }

    public int getOptionIndex(String optionId) {
        if(optionList == null || StringUtils.isBlank(optionId))
            return -1;
        int inex = 0;
        for(OptionItem item : optionList) {
            if(optionId.equalsIgnoreCase(item.optionId))
                return inex;
            inex++;
        }
        return -1;
    }

}
